const axios = require('axios');

// DexScreener API 端点
const DEXSCREENER_API = 'https://api.dexscreener.com/latest/dex/tokens';

// 代币地址
const tokenMint = '7K24mBiSdCdgok4KhWAGJDbXRZKfi7hee3qwgb2qpump';

// 查询 DexScreener API
async function fetchPools(tokenAddress) {
  try {
    console.log(`查询代币 ${tokenAddress} 的池子信息...`);
    const response = await axios.get(`${DEXSCREENER_API}/${tokenAddress}`);
    return response.data.pairs;
  } catch (error) {
    console.error('DexScreener API 查询失败:', error.message);
    return [];
  }
}

// 解析池子类型
function getPoolType(dexId, labels) {
  if (dexId === 'pumpswap') {
    return 'Bonding Curve Pool (PumpSwap)';
  } else if (dexId === 'pump') {
    return 'Bonding Curve Pool (Pump.fun)';
  } else if (dexId === 'raydium') {
    if (labels && labels.includes('CLMM')) {
      return 'Concentrated Liquidity Pool (CLMM)';
    }
    return 'AMM Pool';
  } else if (dexId === 'meteora') {
    if (labels && labels.includes('DLMM')) {
      return 'Dynamic Liquidity Pool (DLMM)';
    }
    return 'Dynamic Pool';
  } else if (dexId === 'orca') {
    return 'Whirlpool';
  }
  return 'Unknown Pool Type';
}

// 解析并输出池子信息
function printPoolInfo(pairs) {
  if (!pairs || pairs.length === 0) {
    console.log('未找到任何池子');
    return;
  }

  console.log(`\n找到 ${pairs.length} 个池子：\n`);
  pairs.forEach((pair, index) => {
    const poolType = getPoolType(pair.dexId, pair.labels);
    console.log(`池子 ${index + 1}:`);
    console.log(`  DEX: ${pair.dexId}`);
    console.log(`  池子类型: ${poolType}`);
    console.log(`  池子地址: ${pair.pairAddress}`);
    console.log(`  基础代币: ${pair.baseToken.symbol} (${pair.baseToken.address})`);
    console.log(`  报价代币: ${pair.quoteToken.symbol} (${pair.quoteToken.address})`);
    console.log(`  价格 (USD): $${pair.priceUsd}`);
    console.log(`  流动性 (USD): $${pair.liquidity.usd}`);
    console.log(`  市值 (USD): $${pair.marketCap}`);
    console.log(`  创建时间: ${new Date(pair.pairCreatedAt).toISOString()}`);
    console.log('------------------------');
  });
}

// 主函数
async function main() {
  const pairs = await fetchPools(tokenMint);
  printPoolInfo(pairs);
}

// 运行
main().catch(error => console.error('程序出错:', error));